<template>
  <div class="com-contatiner">
    <div class="title" :style="comStyle" @click="isShow = !isShow">
      <span>▍ {{showTitle}}</span>
      <span class="iconfont title-icon" :style="comStyle">&#xe6eb;</span>
      <div class="select-con" v-show="isShow">
        <div v-for="item in selectTypes" :key="item.key" @click="handleSelent(item.key)" class="select-type" :style="magerLeft">{{item.text}}</div>
      </div>
    </div>
    <div class="com-chart" ref="trend_ref"></div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getThemeValue } from '@/utils/theme_utils'
export default {
  data () {
    return {
      chertInstane: null, // echarts实例
      allData: '', // 图表的数据
      isShow: false, // 切换图表的显示与隐藏
      choiceType: 'map', // 要显示的title值
      titleFontSize: '', // 屏幕适配的尺寸
      timeId: ''
    }
  },
  created () {
    this.$socket.registerCallBack('trendData', this.getData)
  },
  // 入口函数
  mounted () {
    // this.getData() // 调用获取服务器函数
    this.initChart() // 调用echartsInstance对象函数
    window.addEventListener('resize', this.screenAdapter) // 窗口监听事件
    this.screenAdapter() // 调用屏幕适配的函数
    this.setInter()
    this.$socket.send({
      action: 'getData',
      socketType: 'trendData',
      chartName: 'trend',
      value: ''
    })
  },
  // 销毁函数
  destroyed () {
    window.removeEventListener('resize', this.screenAdapter)
    this.$socket.unRegisterCallBack('trendData')
  },
  methods: {
    // 初始化echartsInstance对象
    initChart () {
      // 创建echartsInstance对象
      this.chertInstane = this.$echarts.init(this.$refs.trend_ref, this.theme)
      // 初始化echarts的数据
      const initOption = {
        grid: {
          left: '3%',
          top: '35%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          boundaryGap: false
        },
        yAxis: {
          type: 'value'
        },
        legend: {
          left: 20,
          top: '15%',
          icon: 'circle'
        },
        tooltip: {
          show: true,
          trigger: 'axis'
        }
      }
      // 展示
      this.chertInstane.setOption(initOption)
    },
    // 获取服务器数据
    getData (res) {
      // axios获取数据
      // const { data: res } = await this.$http.get('trend')
      // 赋值给this.allData
      this.allData = res
      // 调用updataChart方法
      this.updataChart() // 传参的方法也可以 2种方法
      // this.updataChart(res.common.month)
    },
    // 处理数据
    updataChart () {
      // 将月份赋值给timeArr
      const timeArr = this.allData.common.month
      // 半透明的颜色值
      const colorArr1 = ['rgba(11, 168, 44, 0.5)', 'rgba(44, 110, 255, 0.5)', 'rgba(22, 242, 217, 0.5)', 'rgba(254, 33, 30, 0.5)', 'rgba(250, 105, 0, 0.5)']
      // 全透明的颜色值
      const colorArr2 = ['rgba(11, 168, 44, 0)', 'rgba(44, 110, 255, 0)', 'rgba(22, 242, 217, 0)', 'rgba(254, 33, 30, 0)', 'rgba(250, 105, 0, 0)']
      // 通过this.choiceType类型获取数据
      const dataArr = this.allData[this.choiceType].data
      // 通过map遍历dataArr配置series项
      const seruesArr = dataArr.map((val, index) => {
        return {
          type: 'line',
          name: val.name,
          data: val.data,
          stack: 'map',
          areaStyle: {
            // 第二种线性渐变的方法
            color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {
                offset: 0,
                color: colorArr1[index]
              }, // 0的颜色值
              {
                offset: 1,
                color: colorArr2[index]
              } // 100的颜色值
            ])
          }
        }
      })
      const legendArr = dataArr.map(val => {
        return val.name
      })
      // echarts的展示数据
      const dataOption = {
        legend: {
          data: legendArr
        },
        xAxis: {
          data: timeArr
        },
        series: seruesArr
      }
      // 配置setOption
      this.chertInstane.setOption(dataOption)
    },
    // 当浏览器大小发生变化的时候会调用的方法，来完成屏幕的适配
    screenAdapter () {
      // 获取字体大小
      this.titleFontSize = (this.$refs.trend_ref.offsetWidth / 100) * 3.6
      // 配置adapterOption
      const adapterOption = {
        legend: {
          itemWidth: this.titleFontSize,
          itemHeight: this.titleFontSize,
          itemGap: this.titleFontSize,
          textStyle: {
            fontSize: this.titleFontSize / 1.5
          }
        }
      }
      this.chertInstane.setOption(adapterOption)
      // 调用resize方法实现屏幕适配
      this.chertInstane.resize()
    },
    // 将key值赋值给this.choiceType并且调用this.updataChart()更新title
    handleSelent (key) {
      console.log(key)
      this.choiceType = key
      this.updataChart()
    },
    setInter () {
      const a = ['map', 'seller', 'commodity']
      let w = 0
      this.timeId = setInterval(() => {
        if (w > a.length - 1) {
          w = 0
        }
        // eslint-disable-next-line no-unused-expressions
        this.choiceType = a[w++]
        this.updataChart()
      }, 20000)
    }
  },
  computed: {
    // eslint-disable-next-line vue/return-in-computed-property
    // 计算出type值并且根据filter方法筛选出数据
    selectTypes () {
      if (!this.allData) {
        return []
      } else {
        return this.allData.type.filter(val => {
          return val.key !== this.choiceType
        })
      }
    },
    // 显示的标题
    showTitle () {
      if (!this.allData) {
        return ''
      } else {
        return this.allData[this.choiceType].title
      }
    },
    // 字体的大小
    comStyle () {
      return {
        fontSize: this.titleFontSize + 'px',
        color: getThemeValue(this.theme).titleColor
      }
    },
    // 字体的外边距
    magerLeft () {
      return {
        marginLeft: this.titleFontSize + 'px'
      }
    },
    // eslint-disable-next-line no-undef
    ...mapState(['theme'])
  },
  watch: {
    theme () {
      console.log('主题切换了')
      this.chertInstane.dispose() // 销毁当前的图表
      this.initChart() // 重新以最新的主题名称初始化图表对象
      this.screenAdapter() // 完成屏幕的适配
      this.updataChart() // 更新图表的展示
    }
  }
}
</script>

<style scoped>
.com-contatiner {
  position: relative;
}
.title {
  position: absolute;
  z-index: 10;
  left: 20px;
  top: 2%;
  color: #fff;
}
.title-icon {
  margin-left: 10px;
  cursor: pointer;
}
.select-type {
  cursor: pointer;
  background-color: #222733;
}
</style>
